﻿using IDEAMS.core.queue;
using log4net;
using System;
using System.Collections.Generic;

namespace IDEAMS.core.worker
{
    public class WorkerManager
    {
        private static ILog logger = LogManager.GetLogger(typeof(WorkerManager));
        public Dictionary<string, Worker> workers;

        private readonly object locker = new object();


        public WorkerManager()
        {
            workers = new Dictionary<string, Worker>();
        }

        //public int Load(EnvResource envResource)
        //{
        //    int rn = -1;


        //    //如果设置了自动启动
        //    envResource.settingCollection.task.autoStartWhenBoot = true;
        //    if (envResource.settingCollection.task.autoStartWhenBoot)
        //    {
        //        envResource.taskGroupCollection.acqusitionTaskCollection.ForEach(v =>
        //        {
        //            Worker worker = WorkerAction.CreateWorker(v.name, v.type);
        //            if (worker == null)
        //            {
        //                logger.ErrorFormat("任务[{0}]创建失败", v.name);
        //                return false;
        //            }
        //            workers.Add(v.name, worker);

        //            return false;
        //        });

        //        envResource.taskGroupCollection.monitorTaskCollection.ForEach(v =>
        //        {
        //            Worker worker = WorkerAction.CreateWorker(v.name, v.type);
        //            if (worker == null)
        //            {
        //                logger.ErrorFormat("任务[{0}]创建失败", v.name);
        //                return false;
        //            }
        //            workers.Add(v.name, worker);

        //            return false;
        //        });
        //    }

        //    return 0;
        //}

        public void Clear()
        {
            workers.Clear();
        }


        public int Add(string key, Worker worker)
        {
            int rn = 0;
            lock (locker)
            {
                try
                {
                    workers.Add(key, worker);
                }
                catch (Exception)
                {
                    rn = -1;
                }
            }


            return rn;
        }

        public int Delete(string key)
        {
            int rn = 0;
            lock (locker)
            {
                try
                {
                    workers.Remove(key);
                }
                catch (Exception)
                {
                    rn = -1;
                }
            }


            return rn;
        }

        public Worker Find(string key)
        {
            Worker rn = null;
            lock (locker)
            {
                try
                {
                    if (workers.ContainsKey(key))
                    {
                        rn = workers[key];
                    }
                }
                catch (Exception)
                {
                    rn = null;
                }
            }


            return rn;
        }

        public List<T> GetWorkersByType<T>() where T : Worker
        {
            List<T> wkList = new List<T>();

            lock (locker)
            {
                foreach (var wk in workers.Values)
                {
                    if (wk.GetType() == typeof(T))
                    {
                        wkList.Add(wk as T);
                    }
                }
            }

            return wkList;
        }
    }
}
